/**
 * 显示错误信息
 */
function showError(e,msg){
    var alt = e.attr('alt').split(':');
    var name = alt[0];
    var args = alt[1];
    var msg = msg.replace('{name}', name);
    var node = e.parents('span.input');
    /**
	 * 根据alt参数，判断错误提示输出在哪
	 */
    var eid, errArea ;
    if(args.indexOf('errArea')!=-1) {
        var eid = /errArea{(.+?)}/.exec(alt)[1];
        errArea = $("#"+eid);
    }
    try {
        errArea.html('');
    }catch (e) {}
    /**
	 * 根据msg判断是否显示错误提示
	 */
    switch(msg) {
        case "hide":
            errArea.html('<span class="inputacc"><span class="yes"></span></span>');
            if(e.attr('type') == 'password' || e.attr('type') == 'text') {
                node.removeClass('inputGreen');
                node.removeClass('inputRed');
            }
            return;
        case "custom":
            errArea.html('');
            return;
        case "clear":
            errArea.html('');
            return;
        default:
            errArea.html('<span class="inputacc"><span class="error"></span><span class="fRed">'+ msg +'</span></span>');
            if(e.attr('type') == 'password' || e.attr('type') == 'text') {
                node.removeClass('inputGreen');
                node.addClass('inputRed');
                try {
                    if (e.attr('type') == 'password' && e.attr('name') != 'password2') $("#passW").hide();
                }catch (e) {}
            }
            return;
    }
}	
	
/**
 * 检验用户名的合法性
 */
function checkUsername(username){
    var msg = 'hide';
    var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,15}$/;
    if(username == '')
        return '请输入用户名！';
    if (!patrn.exec(username))
        return '用户名格式不正确！';
    return msg;
}
/**
 * 检验邮箱地址格式是否正确
 */
function checkEmail(str){
    var patrn=/^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}\.){1,4}[a-z]{2,4}$/;
    if(str == '')
        return '邮箱地址不能为空！';
    if (!patrn.exec(str))
        return '邮箱地址格式不正确！';
    return 'hide';
}

/**
 * 检验密码的合法性
 */
function checkPassword(str){
    var len=str.length;
    if(str == '')
        return '密码不能为空！';
    if (len>16||len<6)
        return '密码长度不正确！';
    return 'hide';
}

/**
 * 验证密码强度
 */
function chkKeyboard(){
    var e = this;
    try{
        for (var i=1;i<=3;i++) {
            $("#passW" + i).removeClass('passWcurr');
        }
        var password = e.value;
        var Modes = 0;
        var n = password.length;
        for (var i=0;i<n;i++) {
            Modes |= CharMode(password.charCodeAt(i));
        }
        var btotal = bitTotal(Modes);
        if (n >= 10) btotal++;
        switch(btotal) {
            case 1:
                $("#passW1").addClass('passWcurr');
                return;
            case 2:
                $("#passW2").addClass('passWcurr');
                return;
            case 3:
                $("#passW3").addClass('passWcurr');
                return;
            case 4:
                $("#passW3").addClass('passWcurr');
                return;
            default:
                return;
        }
    }catch(e){
        alert(e.description);
    }
};
	
/**
 * 计算密码强度方法1
 */
function CharMode(iN) {
    if (iN >= 65 && iN <= 90) return 2;
    if (iN >= 97 && iN <= 122) return 4;
    else return 1;
}

/**
 * 计算密码强度方法2
 */
function bitTotal(num) {
    var modes = 0;
    for (var i=0;i<3;i++) {
        if (num & 1) modes++;
        num >>>= 1;
    }
    return modes;
}

/**
 * 计算星座
 */
function getConstellation(month,day){
    var s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    var arr=[20,19,21,21,21,22,23,23,23,23,22,22];
    return s.substr(month*2-(day<arr[month-1]?2:0),2);
}

/**
 * 计算属象
 */
function getZodiac(year){
    x = (1901 - year) % 12;
    if (x == 1 || x == -11) {
        return "鼠";
    }
    if (x == 0) {
        return "牛";
    }
    if (x == 11 || x == -1) {
        return "虎";
    }
    if (x == 10 || x == -2) {
        return "兔";
    }
    if (x == 9 || x == -3)  {
        return "龙";
    }
    if (x == 8 || x == -4)  {
        return "蛇";
    }
    if (x == 7 || x == -5)  {
        return "马";
    }
    if (x == 6 || x == -6)  {
        return "羊";
    }
    if (x == 5 || x == -7)  {
        return "猴子";
    }
    if (x == 4 || x == -8)  {
        return "鸡";
    }
    if (x == 3 || x == -9)  {
        return "狗";
    }
    if (x == 2 || x == -10)  {
        return "猪";
    }
}

function setCurrent(link_area_id){
    var url = location.href,i,c,f,h,o;
    var b = new Array();
    var a = $(link_area_id).find("a");
    num = a.length;
    for(i = 0; i < num; ++i)
        if((c = a[i]) && ((f = c.parentNode).tagName === "LI" || (f = f.parentNode).tagName === "LI")) {
            h = c.href;
            if((o = url.indexOf(h)) >= 0) {
                url = url.substr(o);
                if(url.length >= h.length){
                    b[h.length]=f;
                }
            }
        }
    $(b[b.length-1]).addClass("current");
    return;
}

function RandStr() {
    var chars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
    var str = '';
    for(i=0;i<18;i++){
        str += chars.charAt(Math.floor( Math.random()*chars.length));
    }
    return str;
}
/**
 * 全选
 * 容器是table或div里面的table
 *
 * @param    container    DOM element
 */
function selectAll( container_id ) {
    $('#'+container_id+' input[type="checkbox"]').attr('checked','checked');
    markSelected(container_id);
    return true;
}

/**
 * 全不选
 * 容器是table或div里面的table
 *
 * @param    container    DOM element
 */
function unSelectAll( container_id ) {
    $('#'+container_id+' input[type="checkbox"]').attr('checked','');
    markSelected(container_id);
    return true;
}
function markSelected(container_id){
    $('#'+container_id+' input[type=checkbox]:checked').parent().parent().addClass("selected");
    $('#'+container_id+' input[type=checkbox]:not(:checked)').parent().parent().removeClass("selected");
    return true;
}